using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.Web.Controls
{
    [ParseChildren(true)]
    public class Pager : Control
    {
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual PlaceHolder Template { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageButton))]
        public virtual ITemplate PageButtonTemplate { get; set; }
        
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageButton))]
        public virtual ITemplate UnavailablePageButtonTemplate { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(PageButton))]
        public virtual ITemplate SelectedPageButtonTemplate { get; set; }

        public event EventHandler<PaginationCommandEventArgs> PageCommand = delegate { };

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.Template == null)
                return;

            this.Controls.Add(this.Template);
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            var paginationCommandEventArgs = args as PaginationCommandEventArgs;
            if (paginationCommandEventArgs != null)
            {
                this.PageCommand(this, paginationCommandEventArgs);
                return true;
            }

            return base.OnBubbleEvent(source, args);
        }
    }
}